const express = require('express')

const app = express()
app.use(express.urlencoded({ extended: false }))
const db = require('./database/index')

app.post('/api/reguser', (req, res) => {
    // console.log(req.body);
    let { username, password } = req.body
    // 查询
    let sql = `select * from user where username='${username}'`
    db(sql, (err, results) => {
        if (err) {
            return res.send({ status: 1, message: 'sql语法错误' + err.message })
        }
        if (results.length >= 1) {
            return res.send({ status: 1, message: '用户已存在，请更换用户名！' })
        }
        // 添加
        const utility = require('utility')
        password = utility.md5(password)
        let sql = `insert into user set username='${username}',password='${password}'`
        db(sql, (err, results) => {
            if (err) {
                return res.send({ status: 1, message: 'sql语法错误' + err.message })
            }
            if (results.affectedRows !== 1) {
                return res.send({ status: 1, message: '添加失败' })
            }
        })
        res.send({ status: 0, message: '注册成功' })
    })
})

app.listen(3006, () => {
    console.log('http://localhost:3006');
})